﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using RoadTeam.FineFile.Model;


namespace RoadTeam.FineFile.Code
{
    public class XmlOpera
    {
        /// <summary>
        /// 创建更新汇总的XML文档并且写入所有的Excel文件信息
        /// </summary>
        /// <param name="FilePath">XML存放地址</param>
        /// <param name="FileList">Excel文件信息列表</param>
        public static void CreateXmlFile(String FilePath, List<FileInfo> FileList)
        {
            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "no"));

            XmlNode xnl = doc.CreateElement("Root");
            doc.AppendChild(xnl);

            foreach (FileInfo item in FileList)
            {
                XmlNode xn = doc.CreateElement("FileInfo");
                XmlAttribute Xa = doc.CreateAttribute("FileName");
                Xa.Value = item.Name;
                xn.Attributes.Append(Xa);

                Xa = doc.CreateAttribute("HashCode");
                Xa.Value = String.Empty;
                xn.Attributes.Append(Xa);


                Xa = doc.CreateAttribute("CreateDate");
                Xa.Value = item.CreationTime.ToString();
                xn.Attributes.Append(Xa);

                xnl.AppendChild(xn);
            }
            doc.Save(FilePath);
        }



        public static void SaveXmlData(XmlDocument Doc, String XmlFilePath, List<UpdateInfo> li_Update, FileInfo item)
        {
            XmlNode root = Doc.SelectSingleNode("Root");
            XmlNode node = Doc.SelectSingleNode(@"Root/FileInfo[@FileName='" + item.Name + "']");
            #region 创建FileInfo节点 不存在则创建,存在则删除所有子更新信息
            if (null == node)
            {
                XmlNode xn = Doc.CreateElement("FileInfo");
                XmlAttribute Xa = Doc.CreateAttribute("FileName");
                Xa.Value = item.Name;
                xn.Attributes.Append(Xa);

                Xa = Doc.CreateAttribute("HashCode");
                Xa.Value = GetFileInfo.GetHashCode(item.FullName);
                xn.Attributes.Append(Xa);


                Xa = Doc.CreateAttribute("CreateDate");
                Xa.Value = item.CreationTime.ToString();
                xn.Attributes.Append(Xa);

                root.AppendChild(xn);
            }
            else
            {
                node.Attributes["HashCode"].Value = GetFileInfo.GetHashCode(item.FullName);

                foreach (XmlNode chiNode in node.ChildNodes)
                {
                    chiNode.ParentNode.RemoveChild(chiNode);
                }

                node.InnerText = String.Empty;

            }
            #endregion

            foreach (UpdateInfo Info in li_Update)
            {
                XmlNode UpdateInfoNode = Doc.CreateElement("UpdateInfo");
                //xnlUpdate 
                XmlNode xnl = Doc.CreateElement("UpdateDate");
                xnl.InnerText = Info.UpdateDate;
                UpdateInfoNode.AppendChild(xnl);

                xnl = Doc.CreateElement("UpdateContent");
                xnl.InnerText = Info.UpdateContent;
                UpdateInfoNode.AppendChild(xnl);

                xnl = Doc.CreateElement("UpdatePerson");
                xnl.InnerText = Info.UpdatePerson;
                UpdateInfoNode.AppendChild(xnl);

                xnl = Doc.CreateElement("UpdateDesc");
                xnl.InnerText = Info.UpdateDesc;
                UpdateInfoNode.AppendChild(xnl);

                xnl = Doc.CreateElement("UpdateRemark");
                xnl.InnerText = Info.UpdateRemark;
                UpdateInfoNode.AppendChild(xnl);

                xnl = Doc.CreateElement("Path");

                foreach (PathInfo pathinfo in Info.Path)
                {
                    XmlNode TempXnl = Doc.CreateElement("PathInfo");

                    //添加路径属性,用于判断路径是否有重复使用
                    XmlAttribute XaPath = Doc.CreateAttribute("PathInfo");
                    XaPath.Value = pathinfo.UrlPath;
                    TempXnl.Attributes.Append(XaPath);

                    TempXnl.InnerText = pathinfo.UrlPath;
                    xnl.AppendChild(TempXnl);
                }
                UpdateInfoNode.AppendChild(xnl);
                node.AppendChild(UpdateInfoNode);
            }




        }
        /// <summary>
        /// 文件信息插入到XML表中
        /// </summary>
        /// <param name="XmlFilePath">XML所在地址</param>
        /// <param name="item">文件信息</param>
        public static void InSerFileInfoToXml(String XmlFilePath, FileInfo_Mo item)
        {
            XmlDocument doc = LoadXmlDoc(XmlFilePath);
            XmlNode root = doc.SelectSingleNode("Root");
            XmlNode xn = doc.CreateElement("FileInfo");
            XmlAttribute Xa = doc.CreateAttribute("FileName");
            Xa.Value = item.FileName;
            xn.Attributes.Append(Xa);

            Xa = doc.CreateAttribute("HashCode");
            Xa.Value = item.HasCode;
            xn.Attributes.Append(Xa);


            Xa = doc.CreateAttribute("CreateDate");
            Xa.Value = item.CreateDate.ToString();
            xn.Attributes.Append(Xa);

            root.AppendChild(xn);

            doc.Save(XmlFilePath);

        }


        public static void UpdateXmlFileInfo(String XmlFilePath, String FileName, String NewHashCode)
        {
            XmlDocument doc = LoadXmlDoc(XmlFilePath);
            XmlNode xnl = doc.SelectSingleNode("Root/FileInfo[@FileName=\"" + FileName + "\"]");
            if (xnl != null)
                xnl.Attributes["HashCode"].Value = NewHashCode;
            doc.Save(XmlFilePath);
        }




        /// <summary>
        /// 获取XML中记录的Excel文件信息
        /// </summary>
        /// <param name="FilePath">XML所在地址</param>
        /// <returns></returns>
        public static FileInfo_Mo[] ReadFileInfoFromXml(String FilePath)
        {
            XmlDocument doc = LoadXmlDoc(FilePath);

            XmlNodeList LiNode = doc.SelectNodes(@"Root/FileInfo");

            List<FileInfo_Mo> li = new List<FileInfo_Mo>();
            FileInfo_Mo fileinfo = new FileInfo_Mo();

            foreach (XmlNode item in LiNode)
            {
                fileinfo = new FileInfo_Mo();
                fileinfo.FileName = item.Attributes["FileName"].Value;
                fileinfo.HasCode = item.Attributes["HashCode"].Value;
                fileinfo.CreateDate = Convert.ToDateTime(item.Attributes["CreateDate"].Value);
                li.Add(fileinfo);
            }
            return li.ToArray();
        }

        public static XmlDocument LoadXmlDoc(String FilePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);
            return doc;
        }

        public static void SetFileInfoToXml(String FilePath, FileInfo_Mo[] FileInfos)
        {
            XmlDocument doc = LoadXmlDoc(FilePath);
            XmlNodeList LiNode = doc.SelectNodes("FileInfo");

            bool FindFlag = false;

            foreach (FileInfo_Mo item in FileInfos)
            {
                foreach (XmlNode xnl in LiNode)
                {
                    if (0 == String.Compare(xnl.Attributes["FileName"].Value, item.FileName))
                    {
                        FindFlag = true;
                        xnl.Attributes["HashCode"].Value = item.HasCode;
                        xnl.Attributes["CreateDate"].Value = item.CreateDate.ToString();
                        continue;
                    }
                }
                if (!FindFlag)
                {
                    //创建新的文件信息
                    XmlNode xn = doc.CreateElement("FileInfo");
                    XmlAttribute Xa = doc.CreateAttribute("FileName");
                    Xa.Value = item.FileName;
                    xn.Attributes.Append(Xa);

                    Xa = doc.CreateAttribute("HashCode");
                    Xa.Value = item.HasCode;
                    xn.Attributes.Append(Xa);

                    Xa = doc.CreateAttribute("CreateDate");
                    Xa.Value = item.CreateDate.ToString();
                    xn.Attributes.Append(Xa);

                    doc.AppendChild(xn);
                }
                FindFlag = false;
            }
        }


        public static void SetUpdateInfoToXml(String FilePath, UpdateInfo[] UpdateInfos, String[] UpdatePersons)
        {
            XmlDocument doc = LoadXmlDoc(FilePath);
            XmlNode xnlUpdate;
            XmlNode Node = doc.SelectSingleNode("Root");
            XmlNodeList NodeList = doc.SelectNodes("Root/Update");
            if (NodeList.Count == 0)
            {
                // xnlUpdate = doc.CreateNode(XmlNodeType.Element, "Update", String.Empty);
                xnlUpdate = doc.CreateElement("Update");
                Node.AppendChild(xnlUpdate);
            }
            else
            {
                xnlUpdate = NodeList[0];
            }


            //删除对应的更新信息
            NodeList = doc.SelectNodes("Root/Update/UpdateInfo/UpdatePerson");

            foreach (XmlNode item in NodeList)
            {
                if (UpdatePersons.Contains(item.InnerText))
                {
                    //这里删除不了
                    item.ParentNode.ParentNode.RemoveChild(item.ParentNode);
                }
            }
            // doc.Save(FilePath);




            foreach (UpdateInfo item in UpdateInfos)
            {
                XmlNode UpdateInfoNode = doc.CreateElement("UpdateInfo");
                //xnlUpdate 
                XmlNode xnl = doc.CreateElement("UpdateDate");
                xnl.InnerText = item.UpdateDate;
                UpdateInfoNode.AppendChild(xnl);

                xnl = doc.CreateElement("UpdateContent");
                xnl.InnerText = item.UpdateContent;
                UpdateInfoNode.AppendChild(xnl);

                xnl = doc.CreateElement("UpdatePerson");
                xnl.InnerText = item.UpdatePerson;
                UpdateInfoNode.AppendChild(xnl);

                xnl = doc.CreateElement("UpdateDesc");
                xnl.InnerText = item.UpdateDesc;
                UpdateInfoNode.AppendChild(xnl);

                xnl = doc.CreateElement("UpdateRemark");
                xnl.InnerText = item.UpdateRemark;
                UpdateInfoNode.AppendChild(xnl);

                xnl = doc.CreateElement("Path");

                foreach (PathInfo pathinfo in item.Path)
                {
                    XmlNode TempXnl = doc.CreateElement("PathInfo");

                    TempXnl.InnerText = pathinfo.UrlPath;
                    xnl.AppendChild(TempXnl);
                }
                UpdateInfoNode.AppendChild(xnl);
                xnlUpdate.AppendChild(UpdateInfoNode);
            }
            doc.Save(FilePath);
        }


        public static UpdateInfo[] GetUpdateInfoFromXml(String XmlPath)
        {
            XmlDocument doc = LoadXmlDoc(XmlPath);
            XmlNodeList Li_xnl = doc.SelectNodes("Root/FileInfo/UpdateInfo");

            List<UpdateInfo> li = new List<UpdateInfo>();

            foreach (XmlNode item in Li_xnl)
            {
                UpdateInfo ui = new UpdateInfo();
                ui.UpdateDate = item.SelectSingleNode("UpdateDate").InnerText;
                ui.UpdatePerson = item.SelectSingleNode("UpdatePerson").InnerText;
                ui.UpdateContent = item.SelectSingleNode("UpdateContent").InnerText;
                ui.UpdateDesc = item.SelectSingleNode("UpdateDesc").InnerText;
                ui.UpdateRemark = item.SelectSingleNode("UpdateRemark").InnerText;
                List<PathInfo> li_Path = new List<PathInfo>();
                foreach (XmlNode Temp in item.SelectNodes("Path/PathInfo"))
                {
                    PathInfo pi = new PathInfo();
                    pi.UrlPath = Temp.InnerText;
                    li_Path.Add(pi);
                }

                ui.Path = li_Path.ToArray();
                li.Add(ui);
            }
            return li.ToArray();
        }

        public void PrintUpdateSummary(String XmlPath, List<UpdateInfo> li_UpdateInfo, String DateTimeNow)
        {
            //读取对应的XML文档,不存在则创建
            //在文档后面添加更新信息
            XmlDocument Doc = LoadUpdateSummaryXml(XmlPath);
            XmlNode Update = Doc.SelectSingleNode(@"Root/Update");

            foreach (UpdateInfo item in li_UpdateInfo)
            {
                //追加在更新文档末
                XmlNode Node = Doc.CreateElement("UpdateInfo");
                Update.AppendChild(Node);

                XmlNode xn = Doc.CreateElement("UpdatePerson");
                xn.InnerText = item.UpdatePerson;
                Node.AppendChild(xn);

                xn = Doc.CreateElement("Version");
                xn.InnerText = DateTimeNow;

                XmlAttribute xa = Doc.CreateAttribute("VersionCode");
                xa.Value = DateTimeNow;
                xn.Attributes.Append(xa);

                Node.AppendChild(xn);

                xn = Doc.CreateElement("DeveDate");
                xn.InnerText = item.UpdateDate;
                Node.AppendChild(xn);

                xn = Doc.CreateElement("UpdateContent");
                xn.InnerText = item.UpdateContent;
                Node.AppendChild(xn);
            }
            Doc.Save(XmlPath);
        }
        public XmlDocument LoadUpdateSummaryXml(String XmlPath)
        {
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(XmlPath))
            {
                doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "no"));
                XmlNode xnl = doc.CreateElement("Root");
                doc.AppendChild(xnl);

                XmlNode xnls = doc.CreateElement("Update");
                xnl.AppendChild(xnls);
            }
            else
                doc.Load(XmlPath);
            return doc;
        }


        public XmlDocument LoadProjectXml(String XmlPath)
        {
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(XmlPath))
            {
                doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "no"));
                XmlNode xnl = doc.CreateElement("Root");
                doc.AppendChild(xnl);
            }
            else
                doc.Load(XmlPath);
            return doc;
        }


        public void SaveProjectInfo(String XmlPath, ProJectInfo ProInfo)
        {
            XmlDocument doc = new XmlDocument();
            doc = LoadProjectXml(XmlPath);

            XmlNode xnl = doc.SelectSingleNode("Root/DirInfo[@ProName=\"" + ProInfo.ProjectName + "\"]");
            if (xnl != null)
            {
                //修改
                xnl.Attributes["ProjectPath"].Value = ProInfo.ProjectPath;
                xnl.Attributes["BackUpPath"].Value = ProInfo.BackUpPath;
            }
            else
            {
                //新建节点
                XmlNode Root = doc.SelectSingleNode("Root");
                xnl = doc.CreateElement("DirInfo");

                XmlAttribute Xa = doc.CreateAttribute("ProName");
                Xa.Value = ProInfo.ProjectName;
                xnl.Attributes.Append(Xa);

                Xa = doc.CreateAttribute("BackUpPath");
                Xa.Value = ProInfo.BackUpPath;
                xnl.Attributes.Append(Xa);

                Xa = doc.CreateAttribute("ProjectPath");
                Xa.Value = ProInfo.ProjectPath;
                xnl.Attributes.Append(Xa);

                Root.AppendChild(xnl);
            }
            doc.Save(XmlPath);
        }

        public ProJectInfo GetProInfoByName(string XmlPath, String ProjectName)
        {
            XmlDocument doc = LoadProjectXml(XmlPath);

            XmlNode xnl = doc.SelectSingleNode(@"Root/DirInfo[@ProName='" + ProjectName + "']");

            ProJectInfo project = new ProJectInfo();

            project.ProjectName = xnl.Attributes["ProName"].Value;
            project.ProjectPath = xnl.Attributes["ProjectPath"].Value;
            project.BackUpPath = xnl.Attributes["BackUpPath"].Value;

            return project;
        }

        public List<String> GetProjectList(String XmlPath)
        {
            XmlDocument doc = LoadProjectXml(XmlPath);
            XmlNodeList list = doc.SelectNodes(@"Root/DirInfo");
            List<String> li = new List<string>();
            foreach (XmlNode item in list)
            {
                li.Add(item.Attributes["ProName"].Value);
            }
            return li;
        }


        //获取相关的更新信息以及路径
        public void GetRelaPathInfo(List<String> liPath, List<String> liNewPath, String StrPath, String DevDate, String XmlPath, List<UpdateInfo> liUpdate)
        {
            XmlDocument doc = LoadXmlDoc(XmlPath);
            XmlNodeList nodelist = doc.SelectNodes(@"Root/FileInfo/UpdateInfo/Path/PathInfo[@PathInfo='" + StrPath + "']");
            String TempDevDate = String.Empty;
            if (!liNewPath.Contains(StrPath))
                liNewPath.Add(StrPath);

            foreach (XmlNode item in nodelist)
            {
                //这里取消掉查找相关更新文件算法
                //陈柯良 2014年9月16日10:10:54
                //此处算法需要调整
                break;
                TempDevDate = item.ParentNode.ParentNode.ChildNodes[0].InnerText;
                if (String.IsNullOrEmpty(TempDevDate) ) continue;
                if (String.Compare(TempDevDate, DevDate) <= 0) continue;
        
                if (liUpdate.Count(th => th.UpdateContent.Contains(item.ParentNode.ParentNode.ChildNodes[1].InnerText)) > 0) continue;

                UpdateInfo Ui = new UpdateInfo();
                Ui.UpdateDate = item.ParentNode.ParentNode.ChildNodes[0].InnerText;
                Ui.UpdateContent = item.ParentNode.ParentNode.ChildNodes[1].InnerText;
                Ui.UpdatePerson = item.ParentNode.ParentNode.ChildNodes[2].InnerText;
                Ui.UpdateDesc = item.ParentNode.ParentNode.ChildNodes[3].InnerText;
                Ui.UpdateRemark = item.ParentNode.ParentNode.ChildNodes[4].InnerText;
             
                List<PathInfo> li = new List<PathInfo>();
                foreach (XmlNode temps in item.ParentNode.ChildNodes)
                {
                    PathInfo pi = new PathInfo();
                    pi.UrlPath = temps.InnerText;
                    li.Add(pi);

                    if (!liPath.Contains(temps.InnerText + "|" + Ui.UpdateDate))
                        liPath.Add(temps.InnerText + "|" + Ui.UpdateDate);

                    if (!liNewPath.Contains(temps.InnerText))
                        liNewPath.Add(temps.InnerText);
                }
                Ui.Path = li.ToArray();
                liUpdate.Add(Ui);
            }
        }
    }
}
